class: center, middle, inverse, title-slide .title[ # Phase II: Using Our Toolbox ] .subtitle[ ## Module 7: Birds of a Feather ] .author[ ### Dr. Christopher Kenaley ] .institute[ ### Boston College ] .date[ ### 2024/18/11 ] --- # In class today <!-- Add icon library --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"> .pull-left[ Today we'll .... - Review WCR 11 - Consider Linear Mixed-effect Modeling ] .pull-right[  ] --- # Linear Mixed-effect Modeling .pull-left[ - Other models assume independence of data - LME included fixed and random effects - Often termed repeated measures - Same model formulae, but + `+(1|random_effect)` ] .pull-right[ ``` r iris %>% group_by(Species) %>% summarise_all(.funs = mean) ``` ``` ## # A tibble: 3 × 5 ## Species Sepal.Length Sepal.Width Petal.Length Petal.Width ## <fct> <dbl> <dbl> <dbl> <dbl> ## 1 setosa 5.01 3.43 1.46 0.246 ## 2 versicolor 5.94 2.77 4.26 1.33 ## 3 virginica 6.59 2.97 5.55 2.03 ``` ] --- # Linear Mixed-effect Modeling How do a determine what fixed and random effects are? - Depends on the question + what is independent? + what data are dependent, autocorrelated, linked, etc.? ``` r iris %>% group_by(Species) %>% summarise_all(.funs = mean) ``` ``` ## # A tibble: 3 × 5 ## Species Sepal.Length Sepal.Width Petal.Length Petal.Width ## <fct> <dbl> <dbl> <dbl> <dbl> ## 1 setosa 5.01 3.43 1.46 0.246 ## 2 versicolor 5.94 2.77 4.26 1.33 ## 3 virginica 6.59 2.97 5.55 2.03 ``` --- # Linear Mixed-effect Modeling Say our question was, is sepal length different between species? - Species isn't random, it's part of the question ``` r fit <- lm(Sepal.Length~Petal.Length+Species,iris) anova(fit) ``` ``` ## Analysis of Variance Table ## ## Response: Sepal.Length ## Df Sum Sq Mean Sq F value Pr(>F) ## Petal.Length 1 77.643 77.643 679.544 < 2.2e-16 *** ## Species 2 7.843 3.922 34.323 6.053e-13 *** ## Residuals 146 16.682 0.114 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- # Linear Mixed-effect Modeling Say our question was, is sepal length a function of petal length? - Species is now random, it's not part of the question - Random intercepts (`(1|Species)`) . . . ``` r fit_ri <- lmer(Sepal.Length~Petal.Length+(1|Species),iris) library(car) Anova(fit_ri) ``` ``` ## Analysis of Deviance Table (Type II Wald chisquare tests) ## ## Response: Sepal.Length ## Chisq Df Pr(>Chisq) ## Petal.Length 193.96 1 < 2.2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- # Linear Mixed-effect Modeling .pull-left[ Random intercepts (`(1|Species)`) - All species respond to Petal.Length similarly - BUT each species has its own baseline sepal length - Random intercepts = vertical shifts ``` r fit_ri <- lmer(Sepal.Length~Petal.Length+(1|Species),iris) library(sjPlot) p<- plot_model(fit_ri, type = "re", show.values = TRUE) + ggtitle("Random Intercepts: Baseline Differences Among Species") ``` ] .pull-right[ ``` r print(p) ``` <!-- --> ] --- # Linear Mixed-effect Modeling .pull-left[ Random intercepts (`(1|Species)`) - All species respond to Petal.Length similarly - BUT each species has its own baseline sepal length - Random intercepts = vertical shifts ``` r p <- iris %>% ggplot(aes(Petal.Length,Sepal.Length,col=Species))+ geom_point()+ geom_smooth(method="lm") ``` ] .pull-right[ ``` r print(p) ``` ``` ## `geom_smooth()` using formula = 'y ~ x' ``` <!-- --> ] --- # Linear Mixed-effect Modeling Say our question was, is sepal length a function of petal length? - Species is now random, it's not part of the question - Random slopes (`(Petal.Length|Species)`) . . . ``` r fit_rs <- lmer(Sepal.Length~Petal.Length+(Petal.Length|Species),iris) ``` ``` ## boundary (singular) fit: see help('isSingular') ``` ``` r Anova(fit_rs) ``` ``` ## Analysis of Deviance Table (Type II Wald chisquare tests) ## ## Response: Sepal.Length ## Chisq Df Pr(>Chisq) ## Petal.Length 37.082 1 1.133e-09 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- # Linear Mixed-effect Modeling .pull-left[ Random slopes (`(Petal.Length|Species)`) - All species respond to Petal.Length differently - Species vary in how sepal length increases with petal length (slopes) ``` r fit_ri <- lmer(Sepal.Length~Petal.Length+(1|Species),iris) p<- plot_model(fit_rs, type = "re", show.values = TRUE) + ggtitle("Random Slopes: Petal-Length Effect Varies Among Species") ``` ] .pull-right[ ``` r print(p) ``` <!-- --> ] --- # Linear Mixed-effect Modeling .pull-left[ Random slopes (`(Petal.Length|Species)`) - All species respond to Petal.Length differently - Species vary in how sepal length increases with petal length (slopes) ``` r p <- iris %>% ggplot(aes(Petal.Length,Sepal.Length,col=Species))+ geom_point()+ geom_smooth(method="lm") ``` ] .pull-right[ ``` r print(p) ``` ``` ## `geom_smooth()` using formula = 'y ~ x' ``` <!-- --> ]